package com.boshao.classschedule.fragment;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.utils.ViewHolder;

/**
 * Dialog 选择周数
 * @author boshao
 *
 */
public class RepeatDialogFragment extends DialogFragment implements OnClickListener,OnItemClickListener{
	public static final String TAG = "repeat_dialog_fragment";
	private View mRootView;
	private GridView mGridView;
	private TextView mTvCancel,mTvSave;
	public static final String[] WEEKDAYS = {"周日","周一","周二","周三","周四","周五","周六"};
	private Map<Integer,Boolean> mMap = new HashMap<>();
	private TextView mRepeatTextView;
	
	public static RepeatDialogFragment newInstance(TextView repeatTextView){
		RepeatDialogFragment fragment = new RepeatDialogFragment();
		fragment.setRepeatTextView(repeatTextView);
		return fragment;
	}
	
	public void setRepeatTextView(TextView reTextView){
		mRepeatTextView = reTextView;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE,0);
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.dialogfragment_repeat, container,false);
		mGridView = (GridView) mRootView.findViewById(R.id.gv_week);		
		mGridView.setAdapter(new DialogAdapter());
		mGridView.setOnItemClickListener(this);
		mTvCancel = (TextView) mRootView.findViewById(R.id.tv_cancel);
		mTvCancel.setOnClickListener(this);
		mTvSave = (TextView) mRootView.findViewById(R.id.tv_save);
		mTvSave.setOnClickListener(this);
		return mRootView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_save:
			save();
			break;
		case R.id.tv_cancel:
			dismiss();
			break;
		}
	}

	private void save() {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<Integer,Boolean> entry:mMap.entrySet()) {
			if(entry.getValue()){
				sb.append(WEEKDAYS[entry.getKey()]);
				sb.append(",");
			}
		}
		mRepeatTextView.setText(sb.toString());
		dismiss();
	}

	class DialogAdapter extends BaseAdapter{
		public DialogAdapter() {
			initMap();
		}
		
		private void initMap() {
			for(int i = 0;i < WEEKDAYS.length;i++){
				mMap.put(i,false);
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.gridview_weeks,parent, false);
			}
			TextView week = ViewHolder.get(convertView,R.id.tv_week);
			ImageView iv = ViewHolder.get(convertView,R.id.iv_week);
			if(mMap.get(position)){
				iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_circle_shap));
			}else{
				iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.gray_circle_shap));

			}
			week.setText(WEEKDAYS[position]);
			return convertView;
		}			
		@Override
		public long getItemId(int position) {
			return 0;
		}			
		@Override
		public Object getItem(int position) {
			return position;
		}			
		@Override
		public int getCount() {
			return 7;
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(mMap.get(position)){
			mMap.put(position,false);
		}else{
			mMap.put(position,true);
		}
		((DialogAdapter)parent.getAdapter()).notifyDataSetChanged();
	}
}
